// 入驻申请
<template>
   <div class="search">
     <Row>
        <Card>
          <Row class="operation">
            <Button @click="getDataList" icon="md-refresh">刷新</Button>
          </Row>

          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>

          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
     </Row>

     <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="600">
      <Form ref="form" :model="form" :label-width="160" :rules="formValidate" >
        <FormItem label="审核状态" prop="checkStatus">
          <Select v-model="form.checkStatus" style="width: 100%">
            <Option value="CHECK_SUCCESS">同意审核</Option>
            <Option value="CHECK_FAIL">拒绝审核</Option>
          </Select>
        </FormItem>
        <FormItem label="审核意见" prop="checkReason">
          <Input v-model="form.checkReason" style="width: 100%" type="textarea" />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
      </div>
    </Modal>

    <Modal :title="modalTitle" v-model="contractVisible" :mask-closable='false' :width="600">
        <Form ref="contractForm" :model="contractForm" :label-width="100" :rules="contractFormRules" >
            <FormItem label="文档标题" prop="docTitle">
                <Input v-model="contractForm.docTitle" style="width: 100%" />
            </FormItem>
        </Form>
        <div slot="footer">
        <Button type="text" @click="contractVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="contractSubmit">提交</Button>
      </div>
    </Modal>
   </div> 
</template>

<script>
import { uploadFile } from "@/api/index";
import uploadInputPdf from "@/views/my-components/xboot/upload-input-pdf"

export default {
  components: {
    uploadInputPdf
  },
  data(){
    return{
      file: "",
      contractForm: {
        docTitle: "",
        id: ""
      },
      contractFormRules: {
        docTitle: [{ required: true, message: "请输入合同标题", trigger: "blur" }],
      },
      contractVisible: false,
      currentValue: this.value,
      loading: false,
      modalType: 0,
      submitLoading: false,
      total:0,
      modalTitle: "",
      modalVisible: false,
      data:[],//表单数据
      searchForm:{
        pageSize:10,
        pageNumber:1,
      },
      form: {
        id: "",
        checkStatus: "",
        checkReason: "",
      },
      formValidate: {
        checkStatus: [{ required: true, message: "请选择审核状态", trigger: "change" }],
        checkReason: [{ required: false, message: "请输入合同标题", trigger: "blur" }],
      },
      columns: [
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "合同编号",
          key: "contractId",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "甲方合同标题",
          key: "partOneDocTitle",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "甲方交易号",
          key: "partOneTransactionId",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "乙方合同标题",
          key: "partTwoDocTitle",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "乙方交易号",
          key: "partTwoTransactionId",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "审核状态",
          key: "check",
          minWidth: 120,
          sortable: false,
          render: (h,params)=> {
            if (params.row.check === 1) {
              return h("span","待审核")
            } else if (params.row.check === 2) {
              return h("span","审核成功")
            } else if (params.row.check === 3) {
              return h("span","审核失败")
            }
          }
        },
        {
          title: "审核意见",
          key: "checkReason",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "操作",
          width: 360,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    "marginRight": "5px"
                  },
                  on: {
                    click: () => {
                      this.showContract(params.row);
                    }
                  }
                },
                "查看合同"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "default",
                    size: "small"
                  },
                  style: {
                    display: (params.row.check === 2) ? "inline-block" : "none",
                    "marginRight": "5px"
                  },
                  on: {
                    click: () => {
                      this.contractFiling(params.row);
                    }
                  }
                },
                "合同归档"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small"
                  },
                  style: {
                    "marginRight": "5px"
                  },
                  on: {
                    click: () => {
                      this.downloadContract(params.row);
                    }
                  }
                },
                "下载合同"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    "marginRight": "5px",
                    display: (params.row.check === 1) ? "inline-block" : "none"
                  },
                  on: {
                    click: () => {
                      this.checkContract(params.row);
                    }
                  }
                },
                "合同审核"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "default",
                    size: "small"
                  },
                  style: {
                    "marginRight": "5px",
                    display: (params.row.check === 2) ? "inline-block" : "none"
                  },
                  on: {
                    click: () => {
                      this.manuallySigned(params.row);
                    }
                  }
                },
                "手动签署"
              ),
            ]);
          }
        }
      ],
      }
    },
    methods:{
      init(){
        this.getDataList();
      },
      showContract (v) {
        var data = {
            id: v.id
        }
        this.postRequest("/contractSigning/viewContract",data).then(res=> {
            if (res.success) {
                window.open(res.result)
            } else {
                this.$Message.error(res.message)
            }
        })
      },
      
    //   合同归档
      contractFiling (v) {
        this.$Modal.confirm({
            title: "合同归档",
            content: "你确认要该合同归档吗？",
            loading: false,
            onOk: ()=> {
                var data = {
                    id: v.id
                }
                this.postRequest("/contractSigning/contractFiling", data).then(res=> {
                    this.loading = false;
                    if (res.success) {
                        this.$Message.success(res.result.msg);
                        this.getDataList();
                        this.$Modal.remove()
                    } else {
                        this.$Message.error(res.result.msg);
                        this.$Modal.remove()
                    }
                })
            }
        })
      },
      downloadContract (v) {
        this.$Modal.confirm({
            title: "下载合同",
            content: "你确认要下载该合同吗？",
            loading: false,
            onOk: ()=> {
                var data = {
                    id: v.id
                }
                this.postRequest("/contractSigning/downLoadContract", data).then(res=> {
                    this.loading = false;
                    if (res.success) {
                        window.open(res.result)
                        this.$Modal.remove()
                    } else {
                        this.$Message.error(res.message);
                        this.$Modal.remove()
                    }
                })
            }
        })
      },
      manuallySigned (v) {
        this.modalTitle = "手动签署";
        this.$refs.contractForm.resetFields()
        this.contractForm.id = v.id;
        this.contractVisible = true;
      },
      contractSubmit () {
        this.$refs.contractForm.validate(valid=> {
          if (valid) {
            this.submitLoading = true;
            this.postRequest("/contractSigning/extSign", this.contractForm).then(res=> {
                this.submitLoading = false;
                if (res.success) {
                    this.$Message.success("签署成功");
                    this.getDataList();
                    this.contractVisible = false;
                    window.open(res.result)
                } else {
                    this.$Message.error(res.message);
                    this.contractVisible = false;
                }
            })
          }
        })
      },
      checkContract (v) {
        this.form.id = v.id;
        this.modalTitle = "审核合同";
        this.$refs.form.resetFields();
        this.modalVisible = true;
      },
      getDataList(){
        this.loading = true;
        this.postRequest("/contractSigning/getContractSigningList",this.searchForm).then(res =>{
            this.loading = false
            if(res.success){
                var arr = res.result.dataResult;
                for(let val of arr){
                  for(let i in val){
                    (val[i] === null) ? (val[i] = "-") : ""
                  }
                }
                this.data = arr;
                this.total = res.result.totalSize;
            } else {
                this.data = [];
                this.total = 0;
            }
        })
      },
      changeSort(e) {
        this.searchForm.sort = e.key;
        this.searchForm.order = e.order;
        if (e.order === "normal") {
          this.searchForm.order = "";
        }
        this.getDataList();
      },
      changePage(v) {
        this.searchForm.pageNumber = v;
        this.getDataList();
      },
      changePageSize(v) {
        this.searchForm.pageSize = v;
        this.getDataList();
      },
      clearSelectAll(){
        this.$refs.searchForm.resetFields()
        this.getDataList()
      },
      handleSubmit () {
        this.$refs.form.validate(valid=> {
          if (valid) {
            this.submitLoading = true;
            this.postRequest("/contractSigning/checkContractSigning", this.form).then(res=> {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
                this.modalVisible = false;
              } else {
                this.$Message.success(res.message);
                this.modalVisible = false;
              }
            })
          }
        })
      },
    },
    mounted(){
      this.init()
    }
}
</script>

<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>